package com.kvest.scanrecognizer.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import com.kvest.scanrecognizer.R;
import com.kvest.scanrecognizer.service.NetworkService;
import com.kvest.scanrecognizer.ui.fragment.PreviewFragment;
import com.kvest.scanrecognizer.utility.Utils;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 04.10.13
 * Time: 21:32
 * To change this template use File | Settings | File Templates.
 */
public class PreviewActivity extends BaseActivity implements PreviewFragment.OnFileSelectedListener {
    public static final String FILE_PATH_EXTRA = "com.kvest.scanrecognizer.ui.activity.PreviewActivity.FILE_PATH";
    private PreviewFragment previewFragment;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.fragment_container);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            try {
                previewFragment = new PreviewFragment();
                transaction.add(R.id.container, previewFragment);
            } finally {
                transaction.commit();
            }
        } else {
            previewFragment = (PreviewFragment)getSupportFragmentManager().findFragmentById(R.id.container);
        }

        processIntent(getIntent());
    }

    private void processIntent(Intent intent) {
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                handleSendImage(intent); // Handle single image being sent
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                handleSendMultipleImages(intent); // Handle multiple images being sent
            }
        } else if (intent.hasExtra(FILE_PATH_EXTRA)) {
            addToPreview(getIntent().getStringExtra(FILE_PATH_EXTRA));
        }
    }

    void handleSendImage(Intent intent) {
        Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            addToPreview(Utils.getImagePathByUri(this, imageUri));
        }
    }

    void handleSendMultipleImages(Intent intent) {
        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if (imageUris != null) {
            for (Uri imageUri : imageUris) {
                addToPreview(Utils.getImagePathByUri(this, imageUri));
            }
        }
    }

    @Override
    protected void onNewIntent (Intent intent) {
        super.onNewIntent(intent);

        processIntent(intent);
    }

    private void addToPreview(String filePath) {
        previewFragment.addPreview(filePath);
    }

    @Override
    public void onFileSelected(String fileName, int rotation) {
        Intent intent = new Intent(this, NetworkService.class);
        intent.putExtra(NetworkService.ACTION_CODE_EXTRA, NetworkService.ACTION_CODE_SEND_IMAGE);
        intent.putExtra(NetworkService.IMAGE_PATH_EXTRA, fileName);
        intent.putExtra(NetworkService.ROTATION_EXTRA, rotation);
        startService(intent);
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setTitle(R.string.confirmation);
        adb.setMessage(R.string.cancel_file_sending_confirmation);
        adb.setIcon(android.R.drawable.ic_dialog_info);
        adb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        adb.setNegativeButton(android.R.string.no, null);
        adb.create().show();
    }
}
